package com.biaoyoo.www.personapp.activity;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.biaoyoo.www.personapp.R;
import com.biaoyoo.www.personapp.core.BaseListDefineActivity;
import com.biaoyoo.www.personapp.widget.PullToRefreshRecycler;
import com.biaoyoo.www.personapp.widget.pull.FooterSpanSizeLookup;
import com.biaoyoo.www.personapp.widget.pull.ILayoutManager;
import com.biaoyoo.www.personapp.widget.pull.MyGridLayoutManager;

import java.util.ArrayList;

public class SimpleDefineListActivity extends BaseListDefineActivity<String> {
    private ArrayList<String> datalist;

    @Override
    protected void initDatas() {
        datalist = new ArrayList<>();
        for(int i=0;i<50;i++){
            datalist.add("woshi nimeidni 01110"+i);
        }
        mAdapter = new BaseListDefineAdapter();
        mAdapter.setDatalist(datalist);
        super.initDatas();

    }

    @Override
    protected BaseListDefineActivity<String>.BaseDefineViewHolder getDefineViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(SimpleDefineListActivity.this).inflate(R.layout.sample_list_item, parent, false);
        return new SimpleDefineViewHolder(view);
    }

    class SimpleDefineViewHolder extends BaseDefineViewHolder{

        TextView txt;
        public SimpleDefineViewHolder(View itemView) {
            super(itemView);
             txt = (TextView) itemView.findViewById(R.id.tv_simeple_list_item);
        }

        @Override
        public void bindData(int position) {
            txt.setText(mDatalist.get(position).toString());
        }
    }

    @Override
    public void onRresh(final int action) {
        super.onRresh(action);
        mPullRefreshRecycler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if(PullToRefreshRecycler.ACTION_PULL_TO_REFRESH == action){
                    datalist.clear();
                    for(int i=0;i<50;i++){
                        datalist.add("wo shi refresh item is "+i);
                    }
                }else if(PullToRefreshRecycler.ACTION_LOAD_MORE == action){
                    for(int i=0;i<10;i++){
                        if(datalist.size() >60){
                            mPullRefreshRecycler.setIsLoadMoreEnabled(false);
                            break;
                        }
                        datalist.add("load more item "+i);
                    }

                }
                mAdapter.notifyDataSetChanged();
                mPullRefreshRecycler.onRefreshCompleted();
            }
        },3000);

    }

    @Override
    protected ILayoutManager getLayoutManager() {
        MyGridLayoutManager layoutManager = new MyGridLayoutManager(getApplicationContext(), 3);
        FooterSpanSizeLookup lookup = new FooterSpanSizeLookup(mAdapter, 3);
        layoutManager.setSpanSizeLookup(lookup);
        return layoutManager;
    }
}
